package zzf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import zzf.bean.Department;
import zzf.bean.Employee;
import zzf.dao.DepartmentDao;
import zzf.dao.EmployeeDao;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.time.Duration;
import java.util.Collection;
import java.util.Optional;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    DepartmentDao departmentDao;
    @RequestMapping("/emps")
    public String getEmps(Model model){

        Collection<Employee> all = employeeDao.getAll();
        model.addAttribute("emps",all);

        return "list";
    }


    /*
    * 去员工添加页面之前,需要查出所有部门信息,进行展示
    * */
    @RequestMapping("/toaddpage")
    public String toAddPage(Model model){
        //1:查出所有部门
        Collection<Department> departments = departmentDao.getDepartments();

        //2:放在请求域中
        model.addAttribute("depts",departments);
        model.addAttribute("employee",new Employee());

        //3:去添加页面
        return "add";
    }

    /*
    * 添加员工
    * */
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    public String addEmp(Employee employee){

        System.out.println("要添加的员工:" + employee);
        employeeDao.save(employee);
        //返回列表页面,重定向到查询所有员工的请求
        return "redirect:/emps";
    }


    /*
    * 查询员工,来到修改页面回显
    * */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    public String getEmp(@PathVariable("id")Integer id,Model model){
        //1:查出员工信息
        Employee employee = employeeDao.get(id);
        //2:放在请求域中
        model.addAttribute("employee",employee);
        //3:查出所有部门信息放在隐含模型中
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("depts",departments);
        return "edit";
    }

    /*
    * 修改员工
    * */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.PUT)
    public String updateEmp(@ModelAttribute("employee")Employee employee){

        //1:要修改的员工
        System.out.println(employee);

        //2:修改员工(更新,保存二和一)
        employeeDao.save(employee);

        return "redirect:/emps";
    }

    /*
     * 删除员工
     * */
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
    public String deleteEmp(@PathVariable("id")Integer id){

        employeeDao.delete(id);

        return "redirect:/emps";
    }

    @ModelAttribute
    public void myModelAttribute(@RequestParam(value="id",required = false)Integer id,Model model){
        System.out.println("提前运行");
        if (id != null){
            Employee employee = employeeDao.get(id);
            model.addAttribute("employee",employee);
            System.out.println("提前查询员工"+employee);
        }else{
            System.out.println("id为空");
        }
    }
}
